package com.bbzn.device.client.service.emq.order.impl.mq;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.config.EmqCode;
import com.bbzn.device.client.service.PublishService;
import com.bbzn.device.client.service.emq.order.OrderSendStrategy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author wulongbo
 * @Date 2021/12/16 9:48
 * @Version 1.0
 */
@Component
public class BaseMqSendImpl implements OrderSendStrategy {

    @Resource
    private PublishService publishService;

    protected Map map;

    protected String topic;

    protected String getTopic() {
        return this.topic;
    }

    protected void setTopic(String topic) {
        this.topic = topic;
    }

    @Override
    public String checkMap(String language) throws Exception {
        return JSONObject.toJSONString(map);
    }

    @Override
    public void sendMessage(Map map, String language) throws Exception {
        this.map = map;
        String data = checkMap(language);
        Object imei = map.get("imei");
        String topic = EmqCode.sendTopic + imei + EmqCode.endTopic;
        setTopic(topic);
        publishService.sendToMqtt(data, getTopic(), 2);
    }
}
